package com.system.user.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.user.dao.BindDao;
import com.system.user.dao.LightingDao;
import com.system.user.dao.ThresholdDao;
import com.system.user.entity.LightingEntity;
import com.system.user.entityVO.LightingVO;
import com.system.user.service.LightingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("lightingService")
public class LightingServiceImpl implements LightingService {

    @Autowired
    private LightingDao lightingDao;

    @Autowired
    private ThresholdDao thresholdDao;

    @Autowired
    private BindDao bindDao;

    @Override
    public List<LightingEntity> findAll() {
        return lightingDao.selectAll();
    }

    @Override
    public List<LightingEntity> findBySiteOrNumber(LightingEntity lightingEntity) {
        return lightingDao.selectBySiteOrNumber(lightingEntity);
    }

    @Override
    public List<LightingVO> findAllLight(Integer uId, Integer number) {
        return lightingDao.selectByUId(uId, number);
    }

    @Override
    public List<LightingVO> findAllByBNumber(String bNumber, String createTime) {
        List<LightingVO> list = lightingDao.findAllByBNumber(bNumber);
        if (createTime != null && !createTime.equals("")) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (list.get(0).equals(createTime)) {
                return list;
            }
            List<LightingVO> newList = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
                String time = simpleDateFormat.format(list.get(i).getCreateTime());
                if (time.equals(createTime)) {
                    newList.add(list.get(i));
                    list.remove(i);
                    break;
                }
            }
            for (LightingVO l : list) {
                newList.add(l);
            }
            return newList;
        }
        return list;
    }

    @Override
    public String findSite(String bNumber) {
        return lightingDao.selectSiteByNumber(bNumber);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(LightingEntity lighting) throws Exception {
        if (lightingDao.insertLighting(lighting) > 0) {
            lighting.setLightingNumber("l" + lighting.getId());
            if (lightingDao.updateLighting(lighting) > 0) {
                if (thresholdDao.insertThreshold(lighting.getLightingNumber(), null) > 0) {
                    return true;
                }
            }
        }
        throw new Exception();
    }

    @Override
    public boolean update(LightingEntity lighting) {
        if (lightingDao.updateLighting(lighting) > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        LightingEntity lighting = lightingDao.selectById(id);
        if (lightingDao.deleteLighting(id) > 0) {
            if (thresholdDao.deleteThreshold(lighting.getLightingNumber()) > 0) {
                bindDao.deleteBindByBNumber(lighting.getLightingNumber());
                return true;
            }
        }
        return false;
    }
}
